/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package drivemachine.data2;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author positron
 */
public abstract class Entry<T> implements Comparable<Entry> {
	//public String path;
	protected final Entry<T> parent;
	protected String filename;
	protected final List<Entry<T> > children = new ArrayList<>(20);

	protected long selfsize=-1;
	protected long subsize=0;

	public Entry(String rootPath) {
		parent = null;
		//if(rootPath.endsWith("/") || rootPath.endsWith("\\")) rootPath = rootPath.substring(0, rootPath.length()-1);
		filename = Paths.get(rootPath).toString();

	}
	public Entry(Entry<T> parent, String filename) {
		this.parent = parent;
		this.filename = filename;
	}

	public String getFileName() {	return filename; }

	public long getSelfSize() {	return selfsize;}
	public long getSubSize() { return subsize; }
	public long getFullSize() { return selfsize+subsize; }

	//public abstract void traverse(EntryIterator<>);

	public boolean hasChildren() { return !children.isEmpty(); }

	public int getNumChildren() {
		return children.size();
	}

	public Entry getChild(int index) {
		return children.get(index);
	}

	public int indexOfChild(Entry i) {
		return children.indexOf(i);
	}

	@Override
	public String toString() {	return filename;	}

	private static final long PiB = (long)1<<50;
	private static final long TiB = (long)1<<40;
	private static final long GiB = 1<<30;
	private static final long MiB = 1<<20;
	private static final long KiB = 1<<10;

	public static  String sizeToString(long size) {
		//return size+"B";
		boolean neg=false;
		if (size<0) {size=-size; neg=true;}

		float vv=size;String ss="B";
		if (size> PiB) { vv=(float)size/PiB; ss=" PiB"; } else
		if (size> TiB) { vv=(float)size/TiB; ss=" TiB"; } else
		if (size> GiB) { vv=(float)size/GiB; ss=" GiB"; } else
		if (size> MiB) { vv=(float)size/MiB; ss=" MiB"; } else
		if (size> KiB) { vv=(float)size/KiB; ss=" KiB"; }

		return (neg?"-":"")+String.format("%.2f",vv)+ss;
		//return size +" B";
	}
	/*
	private void writeObject(ObjectOutputStream oos) throws IOException {

		oos.defaultWriteObject();

	}*/

	@Override
	public int compareTo(Entry o) {
		long v = ((this.selfsize+this.subsize) - (o.selfsize+o.subsize));
		return (Math.abs(v)<Integer.MAX_VALUE ? (int)v : v>0?1:-1);
	}

}
